package com.uzai.common.utils;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

import java.util.HashMap;
import java.util.Map;

public final class CommonUtil {
    private static final Logger logger = LoggerFactory.getLogger(CommonUtil.class);

    /**
     * 手机号格式化
     * @param mobile
     * @return
     */
    public static String mobileFormat(String mobile){
        try {
            if (StringUtils.isBlank(mobile)){
                return mobile;
            }
            StringBuffer sb = new StringBuffer(mobile);
            sb = sb.replace(3,7,"****");
            return sb.toString();
        } catch (Exception e) {
            logger.error("mobile format error:{}",e);
            return mobile;
        }
    }

    /**
     * 身份证格式化
     * @param idCard
     * @return
     */
    public static String idCardFormat(String idCard){
        try {
            if (StringUtils.isBlank(idCard)){
                return idCard;
            }
            StringBuffer sb = new StringBuffer(idCard);
            sb = sb.replace(3,14,"****");
            return sb.toString();
        } catch (Exception e) {
            logger.error("idCard format error:{}",e);
            return idCard;
        }
    }

    /**
     * 验证中文字符串
     * @param str
     * @return
     */
    public static boolean isChinese(String str) {
        return str.matches("^[\u4e00-\u9fa5]+$");
    }

    /**
     * 手机号验证
     * @param mobile
     * @return
     */
    public static boolean isMobile(String mobile){
        return mobile.matches("^((13)|(14)|(15)|(16)|(17)|(18)|(19))\\d{9}$");
    }

    public static Map<String, String> urlParse(String url) {
        Map<String, String> result = new HashMap<>();
        if (StringUtils.isBlank(url)) {
            return result;
        }
        String[] urlParts = url.split("\\?");
        if (urlParts.length == 1) {
            return result;
        }
        //有参数
        String[] params = urlParts[1].split("&");
        for (String param : params) {
            String[] keyValue = param.split("=");
            if (keyValue!=null && keyValue.length>=2){
                result.put(keyValue[0], keyValue[1]);
            }
        }
        return result;
    }

    /**
     * 随机生成两位数
     * @return
     */
    public static String getTwo(){
        Random rad=new Random();
        String result  = rad.nextInt(100) +"";
        if(result.length()==1){
            result = "0" + result;
        }
        return result;
    }

    /**
     * 生成交易订单号
     * @return
     */
    public static String getTradeNo() {
        String date = new SimpleDateFormat("yyyyMMdd").format(new Date());
        String seconds = new SimpleDateFormat("HHmmss").format(new Date());
        return date+"001"+getTwo()+"00"+seconds+getTwo();
    }

    /**
     * 生成两数范围的随机数
     * @param min
     * @param max
     * @return
     */
    public static int random(int min,int max){
        Random random = new Random();
        int number = random.nextInt(max)%(max-min+1) + min;
        return number;
    }

    /**
     * 数字格式化成字符串
     * @param val
     * @return
     */
    public static String numberFormat(Double val){
        if (val==null){
            return "";
        }
        NumberFormat nf = NumberFormat.getInstance();
        nf.setMaximumFractionDigits(20);
        nf.setGroupingUsed(false);
        return nf.format(val);
    }

    public static String[] splitByLen(String val,int length){
        int totalSize = val.length()/length;
        String[] vals = new String[totalSize];
        for (int i=0;i<totalSize;i++){
            try {
                int begin = i*length;
                int end = (i+1)*length;
                if (begin<val.length()){
                    String str = val.substring(begin,end);
                    if (str.equals("")){
                        break;
                    }
                    vals[i] = str;
                }
            } catch (Exception e) {
                logger.error("err:",e);
                break;
            }
        }
        return vals;
    }
}
